package yf.platform.biz;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.yfsoft.comm.util.StringUtil;

import yf.platform.bo.BizResult;
import yf.platform.ctl.GlobalKey;


public class CommonService extends Service {
	
	public BizResult login(Map<String,String> input) throws Throwable{
		String username = input.get("username");
		String password = input.get("password");
		Map<String,Object> user = this.daoSupport.findOne("usr_userinfo", null, 
				StringUtil.formatSql("ui_username = '{0}' and ui_password = '{1}'", username,password));
		//用户名或密码错误
		if(null == user){
			return rst.setCode(0)
				.setType(BizResult.REDIRECT)
				.setPage("WIDGETS/login.jsp")
				.setReqAttribute("msg", "用户名或密码错误!");
		}
		//普通用户则根据ACL获取业务列表
		
		if(!"admin".equals(username)){
			List<Map<String,Object>> models = this.daoSupport.find(
					"usr_obs o,sys_securityrole r,sys_securitygroup g,sys_r_sg_sr rr,mdl_security ms,sys_model m", 
					"rr.*,m.*",
					"m.m_visible = 1 and o.o_role = r.sr_id and r.sr_id = rr.r_r and g.sg_id = rr.r_g and ms.ts_sg = g.sg_id and ms.ts_model = m.m_id and rr.r_read = 1 and o.o_id = "+user.get("ui_dept"), 
					"m.m_index");
			rst.setSissAttribute("models", models);
		}else{
			rst.setSissAttribute("models", this.daoSupport.find("sys_model m",null,"m.m_visible = 1","m.m_index"));
		}
		
		
		Object dept = user.get("ui_dept");
		Map<String,Object> obs = null;
		
		//获取到用户部门的相应仪表板和配置信息
		if(null!=dept){
			obs = this.daoSupport.findOne("usr_obs", "o_desktop,o_profile", "o_id = "+dept.toString());
			if(obs!=null){
				String tmp_pid = obs.get("o_profile").toString();
				String tmp_did = obs.get("o_desktop").toString();
				obs.clear();
				if(!StringUtil.isNullOrEmpty(tmp_did)){
					obs.put("dashboard", this.daoSupport.find("usr_desktop d,usr_dashboard b", 
							"b.*", "d.d_id = b.d_desktop and d.d_id = "+tmp_did, " b.d_id "));
				}
				
				if(!StringUtil.isNullOrEmpty(tmp_pid)){
					Map<String,Object> tmp_map = this.daoSupport.findOne("usr_profile",null,"p_id = "+tmp_pid);
					if(tmp_map.containsKey("p_profile")){
						obs.put("profile",profile(tmp_map.get("p_profile").toString()));
					}
				}
				//将部门的相应配置信息保存至SESSION中
				rst.setSissAttribute(GlobalKey.OBS, obs);
			}
		}
		//获取所有模块的安全配置组
		//获取用户角色对应的安全配置
		
		//获取用户的权限配置
//		List<Map<String,Object>> role_group = this.daoSupport.find("sys_r_sg_sr rr,usr_obs o", "rr.*", "o.o_id = "+user.get("ui_dept")+" and o.o_role = rr.r_r", null);
//		
//		rst.setSissAttribute(GlobalKey.SECURITY, role_group);
		
		//用户的配置信息
		String pid = user.get("ui_profile").toString();
		if(!StringUtil.isNullOrEmpty(pid)){
			Map<String,Object> tmp_map = this.daoSupport.findOne("usr_profile",null,"p_id = "+pid);
			if(tmp_map.containsKey("p_profile")){
				user.put("ui_profile",profile(tmp_map.get("p_profile").toString()));
			}
		}
		//用户的仪表板信息
		String did = user.get("ui_desktop").toString();
		if(!StringUtil.isNullOrEmpty(did)){
			List<Map<String,Object>> dashboards = this.daoSupport.find("usr_desktop d,usr_dashboard b", 
					"b.*", "d.d_id = b.d_desktop and d.d_id = "+did, " b.d_id ");
			
			if(dashboards.size()<1){
				rst.setSissAttribute(GlobalKey.DASHBOARD, dashboards);
			}else{
				rst.setSissAttribute(GlobalKey.DASHBOARD, obs.get("dashboard"));
			}
		}
		rst.setSissAttribute(GlobalKey.USER, user);
		
		Map<String,Object> todo_num = this.daoSupport.findOne("wf_entry","count(*) as num", 
				"e_status ='process' and e_next_user = "+user.get("ui_id"));
		return rst.setSissAttribute(GlobalKey.TODO_NUM,todo_num.get("num"))
			.setCode(0)
			.setType(BizResult.JUMP)
			.setPage("WIDGETS/main.jsp");
	}

	private Map<String,String> profile(String profileStr){
		return StringUtil.fromJson(profileStr);
	}
	
	public BizResult notice(Map<String,String> input){
		
		List<Map<String, Object>> notices = null;
		notices = this.daoSupport.findByPage("sys_notice",null, "n_status = 1","n_date desc",0,3);
			
		StringBuffer sb = new StringBuffer("<dl>");
		for(Map<String,Object> n:notices){
			sb.append("<dt>").append(n.get("n_title")).append("</dt>");
			sb.append("<dd>").append(n.get("n_content")).append("</dd>");
		}
		sb.append("</dl>");
		return rst.setCode(0)
				.setData(sb.toString())
				.setType(BizResult.ECHO);
	}
	
	public BizResult install(Map<String,String> input){
		rst.setCode(0);
		try {
			this.daoSupport.runScript("init.sql");
		} catch (IOException e) {
			rst.setCode(-1).setMsg(e.toString());
		}
		return rst.setType(BizResult.ECHO);
	}
	
	public BizResult logout(Map<String,String> input){
		return rst.removeSissAttribute(GlobalKey.USER)
				.setCode(0)
				.setType(BizResult.JUMP)
				.setPage("WIDGETS/login.jsp");
	}
	
	public BizResult contact(Map<String,String> input){
		List<Map<String,Object>> users = this.daoSupport.find("usr_userinfo,usr_obs", "*", "ui_dept = o_id and ui_username <> 'admin'", "o_id,ui_position");
		return rst.setCode(0).setReqAttribute("users", users)
				.setType(BizResult.REDIRECT)
				.setPage("WIDGETS/COMMON/contact.jsp");
	}
	
	public BizResult chgpwd(Map<String,String> input){
		Map<String,Object> user = rst.getSissAttribute(GlobalKey.USER);
		if(!user.get("ui_password").equals(input.get("oldpwd"))){
			return rst.setCode(-1).setMsg("密码错误!").setType(BizResult.ECHO);
		}
		if(!input.get("newpwd").equals(input.get("cfmpwd"))){
			return rst.setCode(-2).setMsg("密码不一致!").setType(BizResult.ECHO);
		}
		String newpwd = input.get("newpwd");
		Map<String,String> e = new HashMap<String,String>();
		e.put("ui_password",newpwd);
		this.daoSupport.update(e, "ui_id = "+user.get("ui_id"), "usr_userinfo");
		return rst.setCode(0)
				.setType(BizResult.ECHO);
	}
}
